package cst.neu.yuejian_31.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.adapter.CmessageAdapter;
import cst.neu.yuejian_31.adapter.SmessageAdapter;
import cst.neu.yuejian_31.bean.CmessageBean;
import cst.neu.yuejian_31.bean.SmessageBean;
import cst.neu.yuejian_31.iface.CmessageListener;
import cst.neu.yuejian_31.model.CmessageModel;
import cst.neu.yuejian_31.model.SmessageModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class CmessageFragment extends BaseFragment implements CmessageListener {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private View view=null;
    private List<CmessageBean> list=null;
    private Context context;
    private CmessageAdapter cmessageAdapter;
    private String userId=null;

    public String getUserId()
    {
        SharedPreferences sp;
        sp=context.getSharedPreferences("login", Context.MODE_PRIVATE);
        return  sp.getString("user_id","");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_cmessage, container, false);
        context=this.getActivity();
        userId=getUserId();
        CmessageModel cmessageModel=new CmessageModel();
        cmessageModel.getCmessage(userId,this);
        initView();
        return view;

    }
    private void initView() {
        recyclerView= (RecyclerView) view.findViewById(R.id.recyclerView_cmessage);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        cmessageAdapter=new CmessageAdapter(context);
        cmessageAdapter.setList(list);
        recyclerView.setAdapter(cmessageAdapter);
// 设置Item之间间隔样式
        recyclerView .addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));
    }

    @Override
    public void onSuccess(List<CmessageBean> cmessageBeen) {
        list=cmessageBeen;
        cmessageAdapter.setList(list);
    }

    @Override
    public void onFail(String msg) {

    }

}
